<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ attribute name="id" type="java.lang.String" required="true" description="编号"%>
<%@ attribute name="name" type="java.lang.String" required="true" description="隐藏域名称（ID）"%>
<%@ attribute name="value" type="java.lang.String" required="true" description="隐藏域值（ID）"%>
<%@ attribute name="labelName" type="java.lang.String" required="true" description="输入框名称（Name）"%>
<%@ attribute name="labelValue" type="java.lang.String" required="true" description="输入框值（Name）"%>
<%@ attribute name="title" type="java.lang.String" required="true" description="选择框标题"%>
<%@ attribute name="url" type="java.lang.String" required="true" description="树结构数据地址"%>
<%@ attribute name="checked" type="java.lang.Boolean" required="false" description="是否显示复选框，如果不需要返回父节点，请设置notAllowSelectParent为true"%>
<%@ attribute name="extId" type="java.lang.String" required="false" description="排除掉的编号（不能选择的编号）"%>
<%@ attribute name="isAll" type="java.lang.Boolean" required="false" description="是否列出全部数据，设置true则不进行数据权限过滤（目前仅对Office有效）"%>
<%@ attribute name="notAllowSelectRoot" type="java.lang.Boolean" required="false" description="不允许选择根节点"%>
<%@ attribute name="notAllowSelectParent" type="java.lang.Boolean" required="false" description="不允许选择父节点"%>
<%@ attribute name="module" type="java.lang.String" required="false" description="过滤栏目模型（只显示指定模型，仅针对CMS的Category树）"%>
<%@ attribute name="selectScopeModule" type="java.lang.Boolean" required="false" description="选择范围内的模型（控制不能选择公共模型，不能选择本栏目外的模型）（仅针对CMS的Category树）"%>
<%@ attribute name="allowClear" type="java.lang.Boolean" required="false" description="是否允许清除"%>
<%@ attribute name="allowInput" type="java.lang.Boolean" required="false" description="文本框可填写"%>
<%@ attribute name="cssClass" type="java.lang.String" required="false" description="css样式"%>
<%@ attribute name="cssStyle" type="java.lang.String" required="false" description="css样式"%>
<%@ attribute name="smallBtn" type="java.lang.Boolean" required="false" description="缩小按钮显示"%>
<%@ attribute name="hideBtn" type="java.lang.Boolean" required="false" description="是否显示按钮"%>
<%@ attribute name="disabled" type="java.lang.String" required="false" description="是否限制选择，如果限制，设置为disabled"%>
<%@ attribute name="dataMsgRequired" type="java.lang.String" required="false" description=""%>

<div class="input-append" >
	<input id="${id}Id" name="${name}" class="${cssClass}" type="hidden" value="${value}"/>
	<input id="${id}Url" name="${id}Url" class="${cssClass}" type="hidden" value="${url}"/>
	<input id="${id}Name" name="${labelName}" ${allowInput?'':'readonly="readonly"'} type="text" value="${labelValue}" data-msg-required="${dataMsgRequired}"
		class="${cssClass}" style="${cssStyle}"/><a id="${id}Button" href="javascript:" class="btn ${disabled} ${hideBtn ? 'hide' : ''}" style="${smallBtn?'padding:4px 2px;':''}">&nbsp;<i class="icon-search"></i>&nbsp;</a>&nbsp;&nbsp;
</div>
<script type="text/javascript">
	$("#${id}Button, #${id}Name").click(function(){
		// 是否限制选择，如果限制，设置为disabled
		if ($("#${id}Button").hasClass("disabled")){
			return true;
		}
		var parentId = $("#companyId").val();
		var url = $("#${id}Url").val();
		if(url == ""){
			alert("请先选择要添加的元素");
			return;
		}
        if (url.indexOf("?") >= 0) {
            url += "&parentId="+parentId;
        }else{
            url +="?parentId="+parentId;
        }
		// 正常打开
		top.$.jBox.open("iframe:${ctx}/tag/treeselect?url="+encodeURIComponent(url)+"&module=${module}&checked=${checked}&extId=${extId}&isAll=${isAll}", "<spring:message code='btn.select'/>", 300, 420, {
			ajaxData:{selectIds: $("#${id}Id").val()},buttons:{"<spring:message code='common.confirm'/>":"ok", ${allowClear?"\"<spring:message code='btn.clean'/>\":\"clear\",  ":""}"<spring:message code='menu.Approval.Control.Off'/>":true},submit:function(v, h, f){
				if (v=="ok"){
					var tree = h.find("iframe")[0].contentWindow.tree;//h.find("iframe").contents();
					var ids = [], names = [], nodes = [];
					if ("${checked}" == "true"){
						nodes = tree.getCheckedNodes(true);
					}else{
						nodes = tree.getSelectedNodes();
					}
					for(var i=0; i<nodes.length; i++) {//<c:if test="${checked && notAllowSelectParent}">
						if (nodes[i].isParent){
							continue; // 如果为复选框选择，则过滤掉父节点
						}//</c:if><c:if test="${notAllowSelectRoot}">
						if (nodes[i].level == 0){
							top.$.jBox.tip("不能选择根节点（"+nodes[i].name+"）请重新选择。");
							return false;
						}//</c:if><c:if test="${notAllowSelectParent}">
						if (nodes[i].isParent){
							top.$.jBox.tip("不能选择父节点（"+nodes[i].name+"）请重新选择。");
							return false;
						}//</c:if><c:if test="${not empty module && selectScopeModule}">
						if (nodes[i].module == ""){
							top.$.jBox.tip("不能选择公共模型（"+nodes[i].name+"）请重新选择。");
							return false;
						}else if (nodes[i].module != "${module}"){
							top.$.jBox.tip("不能选择当前栏目以外的栏目模型，请重新选择。");
							return false;
						}//</c:if>
						ids.push(nodes[i].id);
						names.push(nodes[i].name);//<c:if test="${!checked}">
						break; // 如果为非复选框选择，则返回第一个选择  </c:if>
					}
					$("#${id}Id").val(ids.join(",").replace(/u_/ig,""));
					$("#${id}Name").val(names.join(","));
				}//<c:if test="${allowClear}">
				else if (v=="clear"){
					$("#${id}Id").val("");
					$("#${id}Name").val("");
                }//</c:if>
				if(typeof ${id}TreeselectCallBack == 'function'){
					${id}TreeselectCallBack(v, h, f);
				}
			}, loaded:function(h){
				$(".jbox-content", top.document).css("overflow-y","hidden");
			}
		});
		/**
		 * 选择分公司
		 */
		function companyTreeselectCallBack(v, h, f){
			console.log("选择分公司");
			var type = $("#role").val();
			$("#officeId").val("");
			$("#officeName").val("");
			if(type != "3"){
			$("#officeButton").removeClass("disabled");
			}
		}
        /**
         * 选择设备型号
         */
		function typeTreeselectCallBack(v, h, f){
			console.log("选择设备型号");
			if (v=="ok"){
                var tree = h.find("iframe")[0].contentWindow.tree;
                var ids = [], names = [], nodes = [];
                if ("${checked}" == "true") {
                    nodes = tree.getCheckedNodes(true);
                } else {
                    nodes = tree.getSelectedNodes();
                }
                var width = nodes[0].width;
                var height = nodes[0].height;
                var orientation = nodes[0].orientation;
                var orien = nodes[0].orien;

                $("#width").val(width);
                $("#height").val(height);
                $("#orientation").val(orientation);
                $("#displaymode").val(orien);

            } else if (v == "clear") {
                $("#${id}Id").val("");
                $("#${id}Name").val("");
            }
		}

        function templateTypeTreeselectCallBack(v, h, f) {
            console.log("####################  选择设备类型  ####################");
            if (v == "ok") {
                var tree = h.find("iframe")[0].contentWindow.tree;//h.find("iframe").contents();
                var ids = [], names = [], nodes = [];
                if ("${checked}" == "true") {
                    nodes = tree.getCheckedNodes(true);
                } else {
                    nodes = tree.getSelectedNodes();
                }
                var width = parseInt(nodes[0].width);
                var height = parseInt(nodes[0].height);
                var orientation = nodes[0].orientation;
                // 0:横屏 , 1:竖屏
                var orien = nodes[0].orien;

                $("#width").val(width);
                $("#height").val(height);

                $("#orientation").val(orientation);
                $("#displaymode").val(orien);

                // 如果是横屏,宽度 大于 高度
                if (orien === 0 || orien === '0') {
                    if (width >= height) {
                        $(".box-3").css("width", width).css("height", height);
                    } else {
                        $(".box-3").css("width", height).css("height", width);
                    }
                } else {
                    if (height >= width) {
                        $(".box-3").css("height", height).css("width", width);
                    } else {
                        $(".box-3").css("height", width).css("width", height);
                    }
                }
                /*
                // 每个元素的长宽对调,坐标互换
                $.each(eleTempletSourceList, function (index, value, array) {
                    var sourceType = value.sourceType;
                    console.log(sourceType);
                    console.log(eleTempletSourceList[sourceType].resourceId);
                    var resource = eleTempletSourceList[sourceType];

                    var tmp = resource.xloc;
                    resource.xloc = resource.yloc;
                    resource.yloc = tmp;

                    var tmp2 = resource.width;
                    resource.width = resource.height;
                    resource.height = tmp2;

                    $("#" + sourceType).css("top", resource.yloc).css("left", resource.xloc).css("width", resource.width).css("height", resource.height);
                });*/
                console.log(eleTempletSourceList);
            } else if (v == "clear") {
                $("#${id}Id").val("");
                $("#${id}Name").val("");
            }
        }

		/* 选择元素样式 */
        function styleTreeselectCallBack(v, h, f) {
            console.log("444444444444444444444444444444444444");
            if (v == "ok") {
                var tree = h.find("iframe")[0].contentWindow.tree;//h.find("iframe").contents();
                var ids = [], names = [], nodes = [];
                if ("${checked}" == "true") {
                    nodes = tree.getCheckedNodes(true);
                } else {
                    nodes = tree.getSelectedNodes();
                }
                var resourceId = nodes[0].id;
                var resourceName = nodes[0].name;
                var resourceType = nodes[0].sourceType;
                var view = nodes[0].view;

                eleTempletSourceList[resourceType].resourceId = resourceId;
                eleTempletSourceList[resourceType].resourceName = resourceName;
                eleTempletSourceList[resourceType].view = view;

                /*console.log(nodes[0].view);
                console.log(eleTempletSourceList[resourceType]);*/

                if (typeof styleShow == 'function') {
                    styleShow(resourceType, view);
                }
            } else if (v == "clear") {
                $("#${id}Id").val("");
                $("#${id}Name").val("");
            }
        }
	});
</script>